package 力扣.栈;

import java.util.Stack;

public class 逆波兰表达式求值150 {
    public static void main(String[] args) {
        String[] strings = new String[]{"2","1","+","3","*"};
        int re = evalRPN(strings);
        System.out.println(re);
    }

    private static int evalRPN(String[] strings) {
        Stack<Integer> integerStack = new Stack<>();
        int te = 0;
        for (String s : strings) {
            if (isNumber(s)) {
                int val = Integer.parseInt(s);
                integerStack.push(val);
            }else {
                int topper = integerStack.pop();
                int lower = integerStack.pop();
                if (s.equals("+")) {
                    te = lower + topper;
                } else if (s.equals("-")) {
                    te = lower - topper;
                } else if (s.equals("*")) {
                    te = lower * topper;
                } else if (s.equals("/")) {
                    te = lower / topper;
                }
                integerStack.push(te);
            }
        }
        return integerStack.isEmpty() ? te : integerStack.pop();
    }
    public static boolean  isNumber(String token) {
        return !("+".equals(token) || "-".equals(token) || "*".equals(token) || "/".equals(token));
    }
}
